/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.persistence.BaseConstants;
import com.thinkgem.jeesite.common.persistence.BasePage;
import com.thinkgem.jeesite.modules.sys.SysConstants;
import com.thinkgem.jeesite.modules.sys.dao.SysNotifyMapper;
import com.thinkgem.jeesite.modules.sys.entity.SysNotify;
import com.thinkgem.jeesite.modules.sys.entity.SysNotifyRecord;
import com.thinkgem.jeesite.modules.sys.entity.SysUser;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 通知通告Service
 *
 * @author kongxiangsheng
 * @version 2014-05-16
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysNotifyService extends ServiceImpl<SysNotifyMapper, SysNotify> implements IService<SysNotify> {

    @Autowired
    private SysNotifyRecordService sysNotifyRecordService;

    /**
     * 根据id获取（含子表）
     *
     * @param id
     * @return
     */
    public SysNotify getWithChildren(String id) {
        SysNotify sysNotify = this.getById(id);
        sysNotify.setSysNotifyRecordList(sysNotifyRecordService.getBySysNotifyId(id));
        return sysNotify;
    }

    /**
     * 保存数据（含子表）
     *
     * @param sysNotify
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveWithChildren(SysNotify sysNotify) {
        this.saveOrUpdate(sysNotify);

        for (SysNotifyRecord sysNotifyRecord : sysNotify.getSysNotifyRecordList()) {
            if (StringUtils.isBlank(sysNotifyRecord.getId())) {
                sysNotifyRecord.setSysNotifyId(sysNotify.getId());
                sysNotifyRecordService.save(sysNotifyRecord);
                continue;
            }
            if (BaseConstants.DEL_FLAG_NORMAL.equals(sysNotifyRecord.getDelFlag())) {
                // TODO: 有没有办法不用加下面这行
                sysNotifyRecord.setSysNotifyId(sysNotify.getId());
                sysNotifyRecordService.updateById(sysNotifyRecord);
            } else {
                sysNotifyRecordService.removeById(sysNotifyRecord.getId());
            }
        }
    }

    /**
     * 删除数据（含子表数据）
     *
     * @param sysNotify
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteWithChildren(SysNotify sysNotify) {
        this.removeById(sysNotify.getId());
        sysNotifyRecordService.deleteBySysNotifyId(sysNotify.getId());
    }

    public SysNotify getWithNums(String sysNotifyId) {
        SysNotify entity = this.baseMapper.selectById(sysNotifyId);
        return entity;
    }

    /**
     * 获取我的通知数目
     *
     * @return
     */
    public int selectMyNoticeNum() {
        String sysUserId = UserUtils.getCurrentUserId();
        return this.baseMapper.selectMyNoticeNum(sysUserId);
    }

    /**
     * 分页查询我的通知条目
     *
     * @param page
     * @return
     */
    public BasePage<SysNotify> selectMyNotices(BasePage<SysNotify> page) {
        String sysUserId = UserUtils.getCurrentUserId();
        page.setRecords(this.baseMapper.selectMyNotices(page, sysUserId));
        return page;
    }

    /**
     * 获取通知列表
     * @param page
     * @param sysNotify
     * @return
     */
    public BasePage<SysNotify> selectNotices(BasePage<SysNotify> page,SysNotify sysNotify) {
        page.setRecords(this.baseMapper.selectNotices(page, sysNotify));
        return page;
    }


    /**
     * 更新阅读状态
     *
     * @param sysNotifyId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean updateReadFlag(String sysNotifyId) {
        // 获取当前登陆用户
        String sysUserId = UserUtils.getCurrentUserId();

        // 查询阅读记录
        QueryWrapper queryWrapper = new QueryWrapper<SysNotifyRecord>();
        queryWrapper.eq(SysNotifyRecord.COL_USER_ID, sysUserId);
        queryWrapper.eq(SysNotifyRecord.COL_SYS_NOTIFY_ID, sysNotifyId);
        SysNotifyRecord sysNotifyRecord = sysNotifyRecordService.getOne(queryWrapper);

        // 决定是否更新
        if (null == sysNotifyRecord || SysConstants.NotifyReadFlag.READED.equals(sysNotifyRecord.getReadFlag())) {
            return false;
        } else {
            sysNotifyRecord.setReadFlag(SysConstants.NotifyReadFlag.READED);
            sysNotifyRecord.setReadDate(new Date());
            return sysNotifyRecordService.updateById(sysNotifyRecord);
        }
    }
}